package com.gtfrank.web.servlet.day3;

import com.gtfrank.web.servlet.service.IUserService;
import com.gtfrank.web.servlet.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @ProjectName: sudacoursetrainging
 * @ClassName: UserDeleteServlet
 * @PackageName:com.gtfrank.web.servlet.day3
 * @Description: 用户删除
 * @Author: frank
 * @Date: 2021/4/29 14:10
 * @Version v1.0
 */
@WebServlet(value ="/user/userDeleteServlet" )
public class UserDeleteServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //删除完毕之后显示当前数据库所有用户
        try{
            IUserService userService = new UserServiceImpl();
            //获得请求参数
            String id = req.getParameter("userid");
            int userid =  Integer.parseInt(id);
            userService.delete(userid);
            req.getRequestDispatcher("/user/showUserListServlet").forward(req,resp);
        }catch (Exception e){
            e.printStackTrace();
            req.getRequestDispatcher("/user/showUserListServlet").forward(req,resp);
        }
    }
}
